package com.ws.client;

import com.alibaba.fastjson.JSONObject;
import com.ws.client.other.InputSave;
import com.ws.client.utils.LogHelper;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

/**
 * websocket客户端监听类
 *
 * @author 。
 */
public class BaseWebsocketClient extends WebSocketClient {

    public BaseWebsocketClient(URI serverUri) {
        super(serverUri);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        LogHelper.debug("尝试打开连接");
    }

    @Override
    public void onMessage(String s) {
        JSONObject dataObj = JSONObject.parseObject(s);
        String command = dataObj.getString("command");
        LogHelper.debug("command:" + command + "|data:" + s);

        // lgl////////////////////////////

/*
        if(dataObj.getString("pid")!=null) {
            LogHelper.debug("lgl# try to close window "+dataObj.getString("pid"));
            InputSave.closeWin(dataObj.getString("pid"));

        }*/
        ///////////////lgl//////////

        switch (command) {
            //启动空闲进程
            case "start":
                String pid = dataObj.getString("pid");
                new Thread(() -> {
                    //LogHelper.debug("command:" + command );
                    SeleniumHandler.automateCode(pid);
                }).start();
                break;
            //开始填写号码
            case "startPhone":
                new Thread(() -> {
                    LogHelper.debug("command:" + command + ",phone=" +dataObj.getString("phone"));
                    SeleniumHandler.automatePhone(dataObj.getString("pid"), dataObj.getString("phone"));
                }).start();
                break;
        }
        //下载登录到客户端
        if (command.equals("loginAccount")) {
            String pid = dataObj.getString("pid");
            LogHelper.debug("command:" + command );
            new Thread(() -> {
                //解析请求
                RemoteStartClient.startServerBySocket(pid);
            }).start();
        }
        //请求退出客户端
        else if (command.equals("exitUser")) {
            new Thread(() -> {
                String pid = dataObj.getString("pid");
                LogHelper.debug("command:" + command );
                InputSave.closeWin(pid);
            }).start();
        }
        //服务器文本消息
        else if (command.equals("text")) {
            String message = dataObj.getString("message");
            LogHelper.debug("服务器文本消息:" + message);
        }
    }

    @Override
    public void onClose(int i, String s, boolean b) {
        LogHelper.debug("连接已断开:" + s + "|i:" + i + "|b:" + b);
    }

    @Override
    public void onError(Exception e) {
        LogHelper.debug("发生连接错误:" + e.getMessage());
    }

    /**
     * 发送消息到服务器
     *
     * @param command 命令
     */
    public void sendToServer(String command) {
        sendToServer(command, null);
    }

    /**
     * 发送消息到服务器
     *
     * @param command 命令
     * @param data    数据
     */
    public void sendToServer(String command, JSONObject data) {
        if (data == null) {
            data = new JSONObject();
        }
        data.put("token", InputSave.token_computer);
        data.put("clientId", InputSave.clientId);
        data.put("command", command);
        this.send(data.toJSONString());
    }
}

